package com.seocoo.worker.user.login;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.seocoo.worker.R;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.seocoo.worker.base.ImageSelector;
import com.seocoo.worker.base.PermissionUtil;
import com.seocoo.worker.base.entity.EFile;
import com.seocoo.worker.base.widget.AppToolBer;
import com.seocoo.worker.user.login.model.RegisterModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;

import java.io.File;
import java.util.List;

public class RegisterActivity extends MVVMActivity implements View.OnClickListener {

    private AppToolBer appToolBer;
    private ImageView ivPhoto;
    private TextView tvNext;
    private String filePath;

    public static void start(Context context) {
        context.startActivity(new Intent(context, RegisterActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_register_activity);
        initView();
        StatusBarUtil.setTranslucentForImageView(this, 0, appToolBer);
    }

    private void initView() {
        appToolBer = findViewById(R.id.appToolBer);
        ivPhoto = findViewById(R.id.iv_photo);
        tvNext = findViewById(R.id.tv_next);

        appToolBer.getTvLeft().setOnClickListener(this);
        appToolBer.getTvRight().setOnClickListener(this);
        tvNext.setOnClickListener(this);
        ivPhoto.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (appToolBer.getTvLeft() == v) {
            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("确定放弃注册吗？");
            builder.setOkListener((dialog, which) -> {
                dialog.dismiss();
                finish();
            });
            showDialog(builder);

        } else if (appToolBer.getTvRight() == v) {
            // 加盟流程
            getViewModel(RegisterModel.class).getPlatform();
        } else if (ivPhoto == v) {

            PermissionUtil.checkPermissions(this, "请允许访问相册与摄像头", 1011, new PermissionUtil.PermissionCallBack() {
                @Override
                public void onHasPermission() {
                    ImageSelector.create().count(1).showCamera(true).start(RegisterActivity.this, 1010);
                }

                @Override
                public void onFailedPermission() {
                    showDialogToast("授权失败！请");
                }
            }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);


        } else if (tvNext == v) {
            if (TextUtils.isEmpty(filePath)) {
                showToast("请选择头像！");
                return;
            }
            RegisterTwoActivity.start(this, filePath);
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.onClick(appToolBer.getTvLeft());
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1010 && resultCode == Activity.RESULT_OK) {
            List<String> images = ImageSelector.onActivityResult(resultCode, data);

            if (images != null && images.size() > 0) {
                getViewModel(RegisterModel.class).uploadFile(1010, images.get(0));

            }
        }
    }

    @LiveDataMatch
    public void onFileScuess(int requestCode, EFile file) {
        filePath = file.getFileName();
        new ImageLoaderEngine(file.getFullPath()).error(R.drawable.user_register).placeholder(R.drawable.user_register).into(ivPhoto);
    }

    @LiveDataMatch
    public void onPlatform(String msg) {
        PlatformDialog.showDialogUI(this, "加盟流程",msg);
    }
}
